package fr.umlv.metadata;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import fr.umlv.cmis.listview.IconFactory;

// TODO: Auto-generated Javadoc
/**
 * The Class ListCategoryPanel.
 */
public class ListCategoryPanel extends JPanel{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The Constant CATEGORY_SEPARATOR. */
	public static final String CATEGORY_SEPARATOR = ",";

	/** The categories area. */
	private final JTextArea categoriesArea;
	
	/** The button remove cat. */
	final JButton buttonRemoveCat = new JButton("<");
	
	/** The button add cat. */
	final JButton buttonAddCat = new JButton(">");
	
	/** The category list. */
	private final JList categoryList;
	
	/** The dfm. */
	private final DefaultListModel dfm;
	
	/** The cat available. */
	JLabel catAvailable ;
	
	/** The cat choose. */
	JLabel catChoose ;
	
	/** The list scroller left. */
	JScrollPane listScrollerLeft;
	
	/** The list scroller right. */
	JScrollPane listScrollerRight;
	
	/** The tree. */
	JTree tree ;

	/** The bundle. */
	@SuppressWarnings("unused")
	private ResourceBundle bundle ;

	/**
	 * Instantiates a new list category panel.
	 *
	 * @param name the name
	 * @param bundle the bundle
	 */
	public ListCategoryPanel(String name,final ResourceBundle bundle) {
		super();

		this.bundle = bundle;

		setLayout(new FlowLayout());

		tree = new JTree(new CategoryHandlerModel());
		tree.setCellRenderer(new DefaultTreeCellRenderer(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			
			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
				super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
				
				ImageIcon icon = IconFactory.getFolderIcon(16);
				setIcon(icon);
			    if (leaf == true) {
		            setLeafIcon(icon);
		        } else {
		            setClosedIcon(icon);
		            setOpenIcon(icon);
		        }
			    
			    return this;
				
			}	
		});
		
		this.catAvailable = new JLabel(bundle.getString("categories_available"));
		this.catChoose = new JLabel(bundle.getString("categories_chosen"));

		categoriesArea = new JTextArea(5,2);
		categoriesArea.setEditable(false);
		categoriesArea.setName(MetadataResultProducer.CATEGORY_FIELD+name);

		dfm = new DefaultListModel();
		categoryList = new JList(dfm);

		buttonAddCat.addActionListener(new ActionListener() {

			List<Category> listCat = new ArrayList<Category>();
			
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonRemoveCat.setEnabled(true);
				Category cat = (Category) tree.getSelectionPath().getLastPathComponent();
				if(listCat.contains(cat)) return;
				listCat.add(cat);
				addCategory(cat,cat.getId());
			}
			
		});

		buttonRemoveCat.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				int index = categoryList.getSelectedIndex();

				if (!dfm.isEmpty()) { //Select an index.
					Category cat = (Category) dfm.remove(index);
					if(categoriesArea.getText().contains(cat.getId()+","))
						categoriesArea.setText(categoriesArea.getText().replace(cat.getId()+",",""));
					else if(categoriesArea.getText().contains(","+cat.getId()))
						categoriesArea.setText(categoriesArea.getText().replace(","+cat.getId(),""));
					else
						categoriesArea.setText(categoriesArea.getText().replace(cat.getId(),""));
				} 
				
				if(dfm.isEmpty())
					buttonRemoveCat.setEnabled(false);
				
			}});	

		this.listScrollerLeft = new JScrollPane(tree);
		this.listScrollerRight = new JScrollPane(categoryList);
		listScrollerRight.setPreferredSize(listScrollerLeft.getPreferredSize());

		Box panelCatAvailable = Box.createVerticalBox();
		panelCatAvailable.add(catAvailable);
		panelCatAvailable.add(Box.createGlue());
		panelCatAvailable.add(listScrollerLeft);

		Box panelCatChosen = Box.createVerticalBox();
		panelCatChosen.add(catChoose);
		panelCatChosen.add(Box.createGlue());
		panelCatChosen.add(listScrollerRight);

		Box panelAddRemove = Box.createVerticalBox();
		panelAddRemove.add(Box.createGlue());
		panelAddRemove.add(buttonAddCat);
		panelAddRemove.add(buttonRemoveCat);
		panelAddRemove.add(Box.createGlue());
		
		add(panelCatAvailable);
		add(panelAddRemove);
		add(panelCatChosen);
		
		categoriesArea.setVisible(false);
		add(categoriesArea);
	}

	/**
	 * Gets the categories area.
	 *
	 * @return the categories area
	 */
	public JTextArea getCategoriesArea() {
		return categoriesArea;
	}

	/**
	 * Gets the category list.
	 *
	 * @return the category list
	 */
	public JList getCategoryList() {
		return categoryList;
	}

	/**
	 * Adds the category.
	 *
	 * @param cat the cat
	 * @param id the id
	 */
	public void addCategory(Category cat,String id){

		dfm.addElement(cat);	
		if(! categoriesArea.getText().isEmpty()) categoriesArea.setText(categoriesArea.getText()+CATEGORY_SEPARATOR);
		categoriesArea.setText(categoriesArea.getText()+id);

	}

	/**
	 * Adds the category.
	 *
	 * @param id the id
	 */
	public void addCategory(String id){

		dfm.addElement(new Category(null,CategoryUtils. getCategoryNameFromId(id),id));	
		if(! categoriesArea.getText().isEmpty()) categoriesArea.setText(categoriesArea.getText()+CATEGORY_SEPARATOR);
		categoriesArea.setText(categoriesArea.getText()+id);

	}
}
